package org.xiaom.butler.activity;

import java.util.List;

import org.xiaom.butler.R;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.dbService.DepartmentService;
import org.xiaom.butler.dbService.HotelService;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ItemListActivity extends Activity {
	HotelService hs;
	DepartmentService ds;
	List<Department> departments;
	Hotel hotel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hs=new HotelService(this);
		ds=new DepartmentService(this);
		hotel=hs.getCurrentHotel();
		departments=ds.getByHotel(hotel.getId().toString()); 
		setContentView(R.layout.item_list);
		BaseAdapter ba=new BaseAdapter() {
			@Override
			public View getView(int arg0, View arg1, ViewGroup arg2) {
				final Department d=departments.get(arg0);
				View v=getLayoutInflater().from(ItemListActivity.this).inflate(R.layout.list_item, null);
				TextView tv=(TextView) v.findViewById(R.id.txt_item_name);
				v.findViewById(R.id.img_item_title).setBackgroundDrawable(Drawable.createFromPath(d.getImagePath()));
				tv.setText(d.getChName());
				v.findViewById(R.id.btn_show_card).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ItemListActivity.this,CardDetailActivity.class); 
						intent.putExtra("department", d);
						startActivity(intent);
					}
				});
				v.findViewById(R.id.btn_show_item).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent=new Intent(ItemListActivity.this,ItemDetailActivity.class);
						intent.putExtra("department", d);
						startActivity(intent);
					}
				});
				return v;
			}
			@Override
			public long getItemId(int arg0) {
				return departments.get(arg0).getId();
			}
			@Override
			public Object getItem(int arg0) {
				return departments.get(arg0);
			}
			@Override
			public int getCount() {
				return departments.size();
			}
		};
		ListView lv=(ListView) findViewById(R.id.list_item);
		lv.setAdapter(ba); 
	}@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		this.finish();
		return super.onOptionsItemSelected(item);
	}

}
